package com.xinqi.modules.course.clazz.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.clazz.dto.req.*;
import com.xinqi.modules.course.clazz.dto.rsp.ClassGroupResultDTO;
import com.xinqi.modules.course.clazz.service.ClassGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级分组表控制层
 *
 * @author: wenjie.wu
 * @date: 2023/01/17
 */
@Tag(name = "Web-班级分组", description = "班级分组")
@RestController
@RequestMapping("/web/class-group")
@RequiredArgsConstructor
public class ClassGroupWebController {
    private final ClassGroupService classGroupService;

    @Operation(summary = "班级分组表-分页")
    @GetMapping("/page")
    public R<PageInfo<ClassGroupResultDTO>> page(Param pageable, ClassGroupQueryDTO dto) {
        PageInfo<ClassGroupResultDTO> page = classGroupService.page(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "根据父分组查询子分组列表及子分组学生数量")
    @GetMapping("/{pid}/find-children-list")
    public R<List<ClassGroupResultDTO>> findChildrenListByPid(@PathVariable("pid") Long pid) {
        List<ClassGroupResultDTO> list = classGroupService.findChildrenListByPid(pid);
        return R.data(list);
    }

    @Operation(summary = "根据班级列表查询分组树-不包含学生")
    @GetMapping("/{classId}/find-list-by-class")
    public R<List<ClassGroupResultDTO>> findListByClassId(@PathVariable("classId") Long classId) {
        List<ClassGroupResultDTO> list = classGroupService.findListByClassId(classId);
        return R.data(list);
    }

    /**
     * 分组-小组-树，带分页的树
     *
     * @param pageable
     * @param dto
     * @return
     */
    @Operation(summary = "班级分组树包含学生-分页")
    @GetMapping("/sub-children-page-list")
    public R<PageInfo<ClassGroupResultDTO>> subChildrenPageList(Param pageable, ClassGroupQueryDTO dto) {
        PageInfo<ClassGroupResultDTO> page = classGroupService.subChildrenPageList(pageable, dto);
        return R.data(page);
    }

    @Operation(summary = "班级分组表-详情")
    @GetMapping("/{id}")
    public R<ClassGroupResultDTO> findDetailById(@PathVariable("id") Long id) {
        ClassGroupResultDTO dto = classGroupService.findDetailById(id);
        return R.data(dto);
    }

    @Operation(summary = "新增小组")
    @PostMapping
    public R<Long> insert(@Validated @RequestBody ClassGroupCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(classGroupService.insert(dto));
    }

    /**
     * @param stuId student表中的id，并不是真的学生id
     * @return
     */
    @Operation(summary = "根据小组成员id查询小组")
    @GetMapping("/find-by/{stuId}")
    public R<ClassGroupResultDTO> findByGroupStudentId(@PathVariable("stuId") Long stuId) {
        ClassGroupResultDTO dto = classGroupService.findByGroupStudentId(stuId);
        return R.data(dto);
    }

    @Operation(summary = "老师分组")
    @PostMapping("/teacher-grouping")
    public R<Long> teacherGrouping(@Validated @RequestBody ClassGroupCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(classGroupService.teacherGrouping(dto));
    }

    @Operation(summary = "学生自由分组")
    @PostMapping("/student-free-grouping")
    public R<Long> studentFreeGrouping(@Validated @RequestBody ClassGroupCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.data(classGroupService.studentFreeGrouping(dto));
    }

    @Operation(summary = "修改班级分组表")
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") Long id, @Validated @RequestBody ClassGroupUpdateDTO dto) {
        dto.setId(id);
        return R.data(classGroupService.updateById(dto));
    }

    @Operation(summary = "删除班级分组表")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        return R.data(classGroupService.remove(id));
    }

    @Operation(summary = "递归删除分组及学生")
    @DeleteMapping("/{id}/remove")
    public R<Boolean> removeById(@PathVariable("id") Long id) {
        return R.data(classGroupService.remove(id));
    }

    @Operation(summary = "判断分组/小组名称是否重复")
    @GetMapping("/is-exists-name")
    public R<Boolean> isExistsByName(GroupNameIsExistDto dto) {
        return R.data(classGroupService.isExistsByName(dto.getClassId(), dto.getPid(), dto.getGroupName(), dto.getId()));
    }

    @Operation(summary = "分组重命名")
    @PutMapping("/{id}/edit-name")
    public R<Boolean> editGroupName(@PathVariable("id") Long id, @Validated @RequestBody ClassGroupUpdateDTO dto) {
        dto.setId(id);
        return R.data(classGroupService.updateById(dto));
    }

    @Operation(summary = "班级id查询分组列表")
    @GetMapping("/find")
    public R<List<ClassGroupResultDTO>> find(ClassGroupQueryDTO dto) {
        List<ClassGroupResultDTO> list = classGroupService.find(dto);
        return R.data(list);
    }

    @Operation(summary = "班级id查询分组树")
    @GetMapping("/find-tree")
    public R<List<ClassGroupResultDTO>> findTree(ClassGroupQueryDTO dto) {
        List<ClassGroupResultDTO> list = classGroupService.findTree(dto);
        return R.data(list);
    }

    @Operation(summary = "根据父分组id和小组口令查询小组成员信息")
    @GetMapping("/find-by-sign")
    public R<ClassGroupResultDTO> findByPidAndSign(ClassGroupSignQueryDTO dto) {
        ClassGroupResultDTO resultDTO = classGroupService.findByPidAndSign(dto);
        return R.data(resultDTO);
    }

}
